﻿Imports System.Collections.ObjectModel

Partial Public Class SelectScriptGroupDialog

#Region " Construction"
    ''' <summary>
    ''' Creates a new instance of the dialog.
    ''' </summary>
    ''' <param name="scriptGroups">A list of script groups.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal scriptGroups As IEnumerable(Of ScriptGroup))

        If scriptGroups Is Nothing Then
            Throw New ArgumentNullException("scriptGroups")
        End If

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim collection As New ObservableCollection(Of ScriptGroup)

        'Add the items to the collection
        For Each item In scriptGroups
            collection.Add(item)
        Next

        'Bind to the collection
        ScriptGroupsComboBox.ItemsSource = collection

        EnableButtons()

    End Sub
#End Region

#Region " Events"
    Private Sub topBar_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Me.DragMove()
    End Sub

    Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            If Me.SelectedGroup IsNot Nothing Then
                Me.DialogResult = True
                Me.Close()
            End If
        Catch ex As Exception
            ex.Handle()
        End Try
    End Sub

    Private Sub ScriptGroups_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        Try
            EnableButtons()
        Catch ex As Exception
            ex.Handle()
        End Try
    End Sub

    Private Sub window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Try

        Catch ex As Exception
            ex.Handle()
        End Try
    End Sub

    Private Sub CancelButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.Close()
    End Sub
#End Region

#Region " Properties"
    ''' <summary>
    ''' Gets the group selected by the user
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public ReadOnly Property SelectedGroup() As ScriptGroup
        Get
            Return TryCast(ScriptGroupsComboBox.SelectedItem, ScriptGroup)
        End Get
    End Property
#End Region

#Region " Methods"
    ''' <summary>
    ''' Enables the buttons.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EnableButtons()
        OKButton.IsEnabled = ScriptGroupsComboBox.SelectedItem IsNot Nothing
    End Sub
#End Region

End Class
